package net.kosev.battleships;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;

/**
 * Sound manager
 * 
 * @author Nikola
 *
 */
public class Sound {
	
	/**
	 * SoundPool instance
	 */
	private SoundPool mSoundPool = null;
	
	/**
	 * Activity context
	 */
	private Context mContext = null;
	
	/**
	 * Hit sound id
	 */
	public int hitID;
	
	/**
	 * Miss sound id
	 */
	public int missID;
	
	/**
	 * Vibrator instance
	 */
	private Vibrator vibrator = null;
	
	/**
	 * Constructor
	 * 
	 * @param context	Activity context
	 */
	public Sound(Context context) {
		mContext = context;
		mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
		
		hitID = mSoundPool.load(mContext, R.raw.hit, 0);
		missID = mSoundPool.load(mContext, R.raw.miss, 0);
		
		vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
	}
	
	/**
	 * Play sound
	 * 
	 * @param soundID	Sound to play
	 */
	private void play(int soundID) {
		if (Config.enabledSound == true) {
			mSoundPool.play(soundID, 1, 1, 0, 0, 1);
		}
	}
	
	/**
	 * Play hit sound
	 */
	public void playHit() {
		if (Config.enabledVibration == true) {
			vibrator.vibrate(100);
		}
		play(hitID);
	}
	
	/**
	 * Play miss sound
	 */
	public void playMiss() {
		play(missID);
	}
}
